﻿@using Microsoft.AspNetCore.Builder;
@using Microsoft.Extensions.Options;
@using SimpleIdServer.CredentialIssuer.Domains;
@using SimpleIdServer.CredentialIssuer.Website.Stores.CredentialIssuer;
@inherits Fluxor.Blazor.Web.Components.FluxorComponent
@inject TooltipService tooltipService
@inject DialogService dialogService
@inject NotificationService notificationService
@inject IOptions<RequestLocalizationOptions> requestLocalizationOptions
@inject IDispatcher dispatcher

<RadzenStack Gap="1rem">
    <div>
        <RadzenButton class="mb-1" Click="@(args => AddClaim())" Icon="save" Text="@Global.AddCredentialConfigurationClaim" ButtonStyle="ButtonStyle.Primary" Size="ButtonSize.Medium"></RadzenButton>
    </div>
    @foreach(var claim in CredentialConfiguration.Claims)
    {
        <RadzenPanel Collapsed=true AllowCollapse=true>
            <HeaderTemplate>
                <RadzenRow Class="w-100">
                    <RadzenColumn>
                        <RadzenStack JustifyContent=JustifyContent.Left AlignItems=AlignItems.Center Orientation="Orientation.Horizontal">
                            <RadzenText TextStyle="TextStyle.H6">@claim.SourceUserClaimName</RadzenText>
                            <RadzenIcon Icon="arrow_forward"></RadzenIcon>
                            <RadzenText TextStyle="TextStyle.H6">@claim.Name</RadzenText>
                            @if (!string.IsNullOrWhiteSpace(claim.ValueType))
                            {
                                <RadzenBadge>@claim.ValueType</RadzenBadge>
                            }
                            @if (claim.Mandatory == true)
                            {
                                <RadzenIcon MouseEnter=@(args => ShowTooltip(args, Global.IsRequired)) Icon="check_circle_outline"></RadzenIcon>
                            }
                        </RadzenStack>
                    </RadzenColumn>
                    <RadzenColumn>
                        <RadzenStack JustifyContent="JustifyContent.Right" AlignItems="AlignItems.Center" Orientation="Orientation.Horizontal">
                            <RadzenButton Click=@((evt) => DeleteClaim(claim)) Variant="Variant.Text">@Global.Delete</RadzenButton>
                        </RadzenStack>                    
                    </RadzenColumn>
                </RadzenRow>
                <RadzenStack Orientation="Orientation.Horizontal">
                </RadzenStack>
            </HeaderTemplate>
            <ChildContent>
                <RadzenStack Gap="1rem">
                    <div>
                        <RadzenButton class="mb-1" Click="@(args => AddTranslation(claim))" Icon="add" Text="@Global.AddClaimTranslation" ButtonStyle="ButtonStyle.Primary" Size="ButtonSize.Medium"></RadzenButton>
                    </div>
                    <RadzenRow Gap="1rem">
                        @foreach (var translation in claim.Translations)
                        {
                            <RadzenColumn Size="4">
                                <ClaimViewer Translation="@translation" IsReadOnly=true>
                                    <ActionsContent>
                                        <RadzenButton Click=@((evt) => DeleteTranslation(claim, translation)) Variant="Variant.Text">@Global.Delete</RadzenButton>
                                        <RadzenButton Click=@((evt) => UpdateTranslation(claim, translation)) Variant="Variant.Text">@Global.Edit</RadzenButton>
                                    </ActionsContent>
                                </ClaimViewer>
                            </RadzenColumn>
                        }
                    </RadzenRow>
                </RadzenStack>
            </ChildContent>
        </RadzenPanel>
    }
</RadzenStack>

@code {
    [Parameter]
    public Domains.CredentialConfiguration CredentialConfiguration { get; set; }

    protected override void OnParametersSet()
    {
        base.OnParametersSet();
        if (CredentialConfiguration == null) return;
        SubscribeToAction<AddCredentialClaimSuccessAction>((act) =>
        {
            notificationService.Notify(new NotificationMessage { Severity = NotificationSeverity.Success, Summary = Global.CredentialClaimAdded });
            StateHasChanged();
        });
        SubscribeToAction<DeleteCredentialClaimSuccessAction>((act) =>
        {
            notificationService.Notify(new NotificationMessage { Severity = NotificationSeverity.Success, Summary = Global.CredentialClaimRemoved });
            StateHasChanged();
        });
        SubscribeToAction<AddCredentialClaimTranslationSuccessAction>((act) =>
        {
            notificationService.Notify(new NotificationMessage { Severity = NotificationSeverity.Success, Summary = Global.CredentialTranslationAdded });
            StateHasChanged();
        });
        SubscribeToAction<UpdateCredentialClaimTranslationSuccessAction>((act) =>
        {
            notificationService.Notify(new NotificationMessage { Severity = NotificationSeverity.Success, Summary = Global.CredentialTranslationUpdated });
            StateHasChanged();
        });
        SubscribeToAction<DeleteCredentialClaimTranslationSuccessAction>((act) =>
        {
            notificationService.Notify(new NotificationMessage { Severity = NotificationSeverity.Success, Summary = Global.CredentialTranslationRemoved });
            StateHasChanged();
        });
        SubscribeToAction<AddCredentialClaimFailureAction>((act) =>
        {
            notificationService.Notify(new NotificationMessage { Severity = NotificationSeverity.Error, Summary = act.ErrorMessage });
            StateHasChanged();
        });
        SubscribeToAction<AddCredentialClaimTranslationFailureAction>((act) =>
        {
            notificationService.Notify(new NotificationMessage { Severity = NotificationSeverity.Error, Summary = act.ErrorMessage });
            StateHasChanged();
        });
        SubscribeToAction<UpdateCredentialClaimTranslationFailureAction>((act) =>
        {
            notificationService.Notify(new NotificationMessage { Severity = NotificationSeverity.Error, Summary = act.ErrorMessage });
            StateHasChanged();
        });
    }

    void ShowTooltip(ElementReference elementReference, string message)
    {
        tooltipService.Open(elementReference, message);
    }

    async void AddClaim()
    {
        var result = (await dialogService.OpenAsync<CredentialConfigurationClaimModal>(Global.AddCredentialConfigurationClaim, new Dictionary<string, object>
        {
            { "Claim", new CredentialConfigurationClaim() }
        })) as CredentialConfigurationClaim;
        if (result == null) return;
        var act = new AddCredentialClaimAction { Id = CredentialConfiguration.Id, Mandatory = result.Mandatory, Name = result.Name, SourceUserClaimName = result.SourceUserClaimName, ValueType = result.ValueType };
        dispatcher.Dispatch(act);
    }

    async void AddTranslation(CredentialConfigurationClaim claim)
    {
        var result = (await dialogService.OpenAsync<CredentialConfigurationClaimDisplayModal>(Global.AddCredentialConfigurationTranslation, new Dictionary<string, object>
        {
            { "Translation", new CredentialConfigurationTranslation
            {
                Locale = requestLocalizationOptions.Value.DefaultRequestCulture.Culture.ToString()
            }
        }})) as CredentialConfigurationTranslation;
        if (result == null) return;
        var act = new AddCredentialClaimTranslationAction { Id = CredentialConfiguration.Id, ClaimId = claim.Id, Locale = result.Locale, Name = result.Name };
        dispatcher.Dispatch(act);
    }

    async void UpdateTranslation(CredentialConfigurationClaim claim, CredentialConfigurationTranslation translation)
    {
        var result = (await dialogService.OpenAsync<CredentialConfigurationClaimDisplayModal>(Global.UpdateCredentialConfigurationClaim, new Dictionary<string, object>
        {
            { "Translation", translation }
        }) as CredentialConfigurationTranslation);
        if (result == null) return;
        var act = new UpdateCredentialClaimTranslationAction { ClaimId = claim.Id, Id = CredentialConfiguration.Id, Locale = result.Locale, Name = result.Name, TranslationId = translation.Id };
        dispatcher.Dispatch(act);
    }

    void DeleteTranslation(CredentialConfigurationClaim claim, CredentialConfigurationTranslation translation)
    {
        var act = new DeleteCredentialClaimTranslationAction { Id = CredentialConfiguration.Id, ClaimId = claim.Id, TranslationId = translation.Id };
        dispatcher.Dispatch(act);
    }

    void DeleteClaim(CredentialConfigurationClaim claim)
    {
        var act = new DeleteCredentialClaimAction { Id = CredentialConfiguration.Id, ClaimId = claim.Id };
        dispatcher.Dispatch(act);
    }
}